package com.tech.connect.zhaofuwu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ksy.common.model.City;
import com.tech.connect.R;
import com.tech.connect.activity.CityChooseActivity;
import com.tech.connect.fragment.BaseFragment;
import com.tech.connect.model.ItemCatSub;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class ZhaoFuWuLvYouFragment extends BaseFragment {


    private ZhaoFuWuLvYouPeiFragment zhaoFuWuLvYouPeiFragment;
    private ZhaoFuWuLvYouYaoFragment zhaoFuWuLvYouYaoFragment;
    private TextView tvCity;
    private ItemCatSub gender;
    private ItemCatSub age;
    private ItemCatSub distance;
    private ItemCatSub validType;
    private ItemCatSub userStatus;
    private City city;

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_zhaofuwu_lvyou, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }


    private void initView(View view) {
        view.findViewById(R.id.ivBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });
        tvCity = view.findViewById(R.id.tvCity);
        tvCity.setText("切换城市");
//        tvCity.setText(CurrentInfo.getUserInfo().cityName);
        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(CityChooseActivity.class, 10001);

            }
        });
        view.findViewById(R.id.tvOptions).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, ZhaoFuWuOptionsChooseActivity.class);
                intent.putExtra("gender", gender);
                intent.putExtra("age", age);
                intent.putExtra("distance", distance);
                intent.putExtra("validType", validType);
                intent.putExtra("userStatus", userStatus);
                jump2Activity(intent, 10003);
            }
        });

        final View llPei = view.findViewById(R.id.llPei);
        final View llYue = view.findViewById(R.id.llYue);
        final View tvPei = view.findViewById(R.id.tvPei);
        final View tvYue = view.findViewById(R.id.tvYue);
        llPei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llPei.setBackgroundResource(R.drawable.common_shape_corner5_stroke_yellow);
                llYue.setBackgroundResource(R.drawable.common_shape_corner5_stroke_white);
                tvPei.setSelected(true);
                tvYue.setSelected(false);
                showFragment(0);
            }
        });
        llYue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llPei.setBackgroundResource(R.drawable.common_shape_corner5_stroke_white);
                llYue.setBackgroundResource(R.drawable.common_shape_corner5_stroke_yellow);
                tvPei.setSelected(false);
                tvYue.setSelected(true);
                showFragment(1);
            }
        });
        llPei.performClick();

        view.findViewById(R.id.container);

    }

    private void showFragment(int index) {
        if (index == 0) {
            if (zhaoFuWuLvYouPeiFragment == null) {
                zhaoFuWuLvYouPeiFragment = new ZhaoFuWuLvYouPeiFragment();
                Bundle bundle=new Bundle();
                bundle.putSerializable("city",city);
                zhaoFuWuLvYouPeiFragment.setArguments(bundle);
                addFragment(R.id.container, zhaoFuWuLvYouPeiFragment);
            }
            showFragment(zhaoFuWuLvYouPeiFragment);
            hideFragment(zhaoFuWuLvYouYaoFragment);

        } else if (index == 1) {
            if (zhaoFuWuLvYouYaoFragment == null) {
                zhaoFuWuLvYouYaoFragment = new ZhaoFuWuLvYouYaoFragment();
                Bundle bundle=new Bundle();
                bundle.putSerializable("city",city);
                zhaoFuWuLvYouYaoFragment.setArguments(bundle);
                addFragment(R.id.container, zhaoFuWuLvYouYaoFragment);
            }
            hideFragment(zhaoFuWuLvYouPeiFragment);
            showFragment(zhaoFuWuLvYouYaoFragment);

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == Activity.RESULT_OK && intent != null) {
            if (requestCode == 10001) {
                //城市切换
                Serializable data = intent.getSerializableExtra("data");
                if (data != null && data instanceof City) {
                    city = (City) data;
                    tvCity.setText(city.name);
                    if (zhaoFuWuLvYouPeiFragment != null) {
                        zhaoFuWuLvYouPeiFragment.onCityClick(city);
                    }
                    if (zhaoFuWuLvYouYaoFragment != null) {
                        zhaoFuWuLvYouYaoFragment.onCityClick(city);
                    }
                }
            } else if (requestCode == 10003) {
                //条件切换
                Map<String, Object> map = new HashMap<>();
                Serializable genderSer = intent.getSerializableExtra("gender");
                Serializable ageSer = intent.getSerializableExtra("age");
                Serializable distanceSer = intent.getSerializableExtra("distance");
                Serializable validTypeSer = intent.getSerializableExtra("validType");
                Serializable userStatusSer = intent.getSerializableExtra("userStatus");
                if (genderSer != null && genderSer instanceof ItemCatSub) {
                    gender = (ItemCatSub) genderSer;
                    map.put("gender", gender.value);
                }
                if (ageSer != null && ageSer instanceof ItemCatSub) {
                    age = (ItemCatSub) ageSer;
                    map.put("age", age.value);
                }
                if (distanceSer != null && distanceSer instanceof ItemCatSub) {
                    distance = (ItemCatSub) distanceSer;
                    map.put("distance", distance.value);
                }
                if (validTypeSer != null && validTypeSer instanceof ItemCatSub) {
                    validType = (ItemCatSub) validTypeSer;
                    map.put("validType", validType.value);
                }
                if (userStatusSer != null && userStatusSer instanceof ItemCatSub) {
                    userStatus = (ItemCatSub) userStatusSer;
                    map.put("userStatus", userStatus.value);
                }

                if (zhaoFuWuLvYouPeiFragment != null) {
                    zhaoFuWuLvYouPeiFragment.onOptClick(map);
                }
                if (zhaoFuWuLvYouYaoFragment != null) {
                    zhaoFuWuLvYouYaoFragment.onOptClick(map);
                }
            } else {

            }
        }
    }

}